package com.huifaapp.mall.activity;

import android.annotation.SuppressLint;
import android.app.AlertDialog;
import android.app.Dialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.drawable.BitmapDrawable;
import android.os.Handler;
import android.os.Message;
import android.provider.MediaStore;
import android.text.Editable;
import android.text.TextWatcher;
import android.util.Log;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.view.WindowManager;
import android.view.animation.AnimationUtils;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.GridView;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.PopupWindow;
import android.widget.TextView;

import com.blankj.utilcode.util.SPUtils;
import com.blankj.utilcode.util.ToastUtils;
import com.bumptech.glide.Glide;
import com.google.gson.Gson;
import com.huifaapp.mall.R;
import com.huifaapp.mall.afinal.SPConstants;
import com.huifaapp.mall.api.ApiManager;
import com.huifaapp.mall.bean.AfterSaleStateBean;
import com.huifaapp.mall.selectImage.SelectImageView;
import com.huifaapp.mall.uitils.AUniqueIdentifier;
import com.huifaapp.mall.uitils.Bimp;
import com.huifaapp.mall.uitils.FileUtils;
import com.squareup.okhttp.Request;
import com.zhy.http.okhttp.OkHttpUtils;
import com.zhy.http.okhttp.callback.StringCallback;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

/**
 * Created by Alienware on 2018/1/12.
 */

public class AfterSaleStateActivity extends BaseActivity {
    private   Button activity_aftersale_onlyrefunds, activity_aftersale_barter,
            activity_aftersale_refundofrefunds, activity_aftersale_service;
    private  View selectMenuLayout, tkMoneyLayout;
    private LinearLayout ll_moneytop,ll_moneybottem;
    private List<Button> buttons = new ArrayList<>();
    private EditText inputReson;
    private TextView inputNumText;
    private TextView tv_hwzt,tv_tkyy,tv_sqyy;
    private ImageView im_shop;
    private TextView goodsname,shop_yj,shop_xj,shop_gjhj,shop_colorsize,shop_tkmoney,shop_tkbigmoney;
    private AfterSaleStateBean afterSaleStateBean;
    private AfterSaleStateBean.DataBean dataBean;
    private AfterSaleStateBean.DataBean.GoodsDetailVoBean goodsDetailVoBeen;
    private Button bt_sumbit;
    private String orderno,skuId,goodsid,type,reason,reason1,ed_inputreson;
    private GridView grid_aftersale;
    private GridAdapter adapter;
    private ImageView iv_allorder_back;
    String r = "";

    SelectImageView selectImageView;


    View shopStateLayout, moneyResonLayout, weixiuResonLayout;
    String[] shopStateArr = {"未收到货", "已收到货"};
    String[] shopMoneyState = {"多拍／拍错／不想要", "快递一直未到达", "未按约定时间发货", "快递无跟踪记录"};
    String[] weixiuState = {"质量问题", "其他"};
    int selectShopState = -1;
    int selectMoneyState = -1;
    int weixiuResonState = -1;

    boolean isShopRecived = true;

    @Override
    protected int getLayoutId() {
        return R.layout.activity_after_sale_state;
    }

    @Override
    protected void initialize() {


    }

    @Override
    protected void initView() {
        iv_allorder_back= (ImageView) findViewById(R.id.iv_allorder_back);
        iv_allorder_back.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });
        grid_aftersale = (GridView) findViewById(R.id.state_gridview);
        activity_aftersale_onlyrefunds = (Button) findViewById(R.id.activity_aftersale_onlyrefunds);
        activity_aftersale_barter = (Button) findViewById(R.id.activity_aftersale_barter);
        activity_aftersale_refundofrefunds = (Button) findViewById(R.id.activity_aftersale_refundofrefunds);
        activity_aftersale_service = (Button) findViewById(R.id.activity_aftersale_service);
        ll_moneytop = (LinearLayout) findViewById(R.id.activity_aftersale_smallrefund);
        ll_moneybottem = (LinearLayout) findViewById(R.id.activity_aftersale_bigrefund);
        shop_tkmoney = (TextView) findViewById(R.id.sale_state_tkmoney);
        tv_hwzt = (TextView) findViewById(R.id.after_state_hwzt);
        tv_tkyy = (TextView) findViewById(R.id.after_state_tkyy);
        tv_sqyy = (TextView) findViewById(R.id.after_state_sqyy);
        im_shop = (ImageView) findViewById(R.id.shopImage);
        shop_xj = (TextView) findViewById(R.id.sale_state_xj);
        shop_yj = (TextView) findViewById(R.id.sale_state_yj);
        goodsname = (TextView) findViewById(R.id.sale_state_goodsname);
        shop_gjhj = (TextView) findViewById(R.id.sale_state_gjhj);
        shop_colorsize = (TextView) findViewById(R.id.sale_state_colorsize);




        selectImageView = new SelectImageView();
        selectImageView.setMaxNum(3);
        getSupportFragmentManager().beginTransaction().add(R.id.fragmentContent, selectImageView).commit();
        //reason = "你好啊";
        shop_tkbigmoney = (TextView) findViewById(R.id.sale_state_bigmoney);
        bt_sumbit = (Button) findViewById(R.id.sale_state_btsubmit);
        buttons.add(activity_aftersale_onlyrefunds);
        buttons.add(activity_aftersale_barter);
        buttons.add(activity_aftersale_refundofrefunds);
        buttons.add(activity_aftersale_service);

        //是否已经收到货

        Intent intent = getIntent();

        String state1 = intent.getStringExtra("state");
        Log.e("ffffff",state1+"");
        if(state1 == null){
            return;
        }
        if (state1.equals("3")||state1.equals("4")){
            isShopRecived = false;
        }else {
            isShopRecived = true;
        }

        // s = 12;
        if (isShopRecived==false) {
            activity_aftersale_barter.setVisibility(View.GONE);
            activity_aftersale_service.setVisibility(View.GONE);
        } else {
            activity_aftersale_barter.setVisibility(View.VISIBLE);
            activity_aftersale_service.setVisibility(View.VISIBLE);
        }

        selectMenuLayout = findViewById(R.id.selectMenuLayout);
        tkMoneyLayout = findViewById(R.id.tkMoneyLayout);
        inputReson = (EditText) findViewById(R.id.sale_state_inputReson);
        inputNumText = (TextView) findViewById(R.id.inputNumText);
        shopStateLayout = findViewById(R.id.shopStateLayout);
        moneyResonLayout = findViewById(R.id.moneyResonLayout);
        weixiuResonLayout = findViewById(R.id.weixiuResonLayout);
        adapter = new GridAdapter(this);
        adapter.update();
//        grid_aftersale.setAdapter(adapter);
//        grid_aftersale.setOnItemClickListener(new AdapterView.OnItemClickListener() {
//
//            public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
//                                    long arg3) {
//                if (arg2 == Bimp.bmp.size()) {
//                    new PopupWindows(AfterSaleStateActivity.this, grid_aftersale);
//                } else {
//                    Intent intent = new Intent(AfterSaleStateActivity.this,
//                            PhotoActivity.class);
//                    intent.putExtra("ID", arg2);
//                    startActivity(intent);
//                }
//            }
//        });



        for (int i = 0; i < buttons.size(); i++) {
            buttons.get(i).setOnClickListener(onClickListener);
        }

        inputReson.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {}
            @Override
            public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {}
            @Override
            public void afterTextChanged(Editable editable) {
                String text = inputReson.getText().toString();
                inputNumText.setText(text.length() + "/500");
            }
        });


        shopStateLayout.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                show(selectShopState, shopStateArr, "货物状态", new View.OnClickListener() {
                    @Override
                    public void onClick(View view) {
                        selectShopState = (int) view.getTag();
                        tv_hwzt.setText(shopStateArr[selectShopState]);

                    }
                });

            }
        });

        moneyResonLayout.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                show(selectMoneyState, shopMoneyState, "退款原因", new View.OnClickListener() {
                    @Override
                    public void onClick(View view) {
                        selectMoneyState = (int) view.getTag();
                        tv_tkyy.setText(shopMoneyState[selectMoneyState]);
                        reason = shopMoneyState[selectMoneyState];

                    }
                });
            }
        });

        weixiuResonLayout.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                show(weixiuResonState, weixiuState, "申请原因", new View.OnClickListener() {
                    @Override
                    public void onClick(View view) {
                        weixiuResonState = (int) view.getTag();
                        tv_sqyy.setText(weixiuState[weixiuResonState]);
                        reason1 = weixiuState[weixiuResonState];

                    }
                });
            }
        });

        /*
        *
        * 展示数据
        * */

        Intent intent1 = getIntent();
        orderno = intent1.getStringExtra("orderNo");
        skuId = intent1.getStringExtra("skuId");
        //goodsid = intent.getStringExtra("goodsid");
        Log.e("售后orderno",orderno);
        Log.e("售后sku",skuId);
        OkHttpUtils.get().url(ApiManager.BASE_NIUPAI_URL+"orders/applyindex")
                .addParams("token", SPUtils.getInstance().getString(SPConstants.KEY_USER_TOKEN))
                .addParams("appId", AUniqueIdentifier.getDeviceId(AfterSaleStateActivity.this))
                .addParams("orderNo",orderno)
                .addParams("skuId",skuId)
                .build()
                .execute(new StringCallback() {
                    @Override
                    public void onError(Request request, Exception e) {
                        ToastUtils.showShort("获取失败");
                    }

                    @Override
                    public void onResponse(String response) {
                        try{
                            Gson gson = new Gson();
                            afterSaleStateBean = gson.fromJson(response,AfterSaleStateBean.class);
                            dataBean = afterSaleStateBean.getData();
                            goodsDetailVoBeen = dataBean.getGoodsDetailVo();
                            AfterSaleStateBean.DataBean.GoodsDetailVoBean.ParamBean param = goodsDetailVoBeen.getParam();
                            Glide.with(AfterSaleStateActivity.this).load(goodsDetailVoBeen.getGoodsImg()).into(im_shop);
                            goodsname.setText(goodsDetailVoBeen.getGoodsName());
                            shop_xj.setText("¥"+goodsDetailVoBeen.getOrginPrice()+"");
                            shop_yj.setText("¥"+goodsDetailVoBeen.getPrice()+"");
                            String color = param.getColor() == null ? "无" : param.getColor();   //三元运算符   判断param.getColor()是否为null  如果是null则取 : 前面的值，否则后面的值
                            String brand = param.getSize() == null ? "**" : param.getSize();   //同上
                            shop_colorsize.setText("颜色分类:"+color+";尺寸:"+brand);
                            shop_gjhj.setText("共计"+dataBean.getTotalCount()+"件商品   合计: ¥"+dataBean.getSumAmount()+"(含运费¥"+dataBean.getExpressFee()+")");
                            shop_tkmoney.setText(dataBean.getRefundAmount()+"");
                            shop_tkbigmoney.setText(dataBean.getRefundAmount()+"");


                        }catch (Exception e){
                            ToastUtils.showShort("获取失败");
                        }


                    }
                });



        //提交按钮
        bt_sumbit.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {

                ed_inputreson = inputReson.getText().toString();

                if (clickType == 0 || clickType == 2) {
                    r = reason;
                } else {
                    r = reason1;
                }

                try {
                    if (r.equals("")||ed_inputreson.equals("")||type.equals("")){
                        ToastUtils.showShort("请您填写完整");
                    }

                    else {
                        try {
                            OkHttpUtils.post().url(ApiManager.BASE_NIUPAI_URL+"orders/addafterSaleapply")
                                    .addParams("appId",AUniqueIdentifier.getDeviceId(AfterSaleStateActivity.this))
                                    .addParams("token",SPUtils.getInstance().getString(SPConstants.KEY_USER_TOKEN))
                                    .addParams("orderNo",orderno)
                                    .addParams("skuId",skuId)
                                    .addParams("reason",r)
                                    .addParams("amount",dataBean.getRefundAmount()+"")
                                    //.addParams("img",)
                                    .addParams("content",ed_inputreson)
                                    .addParams("type",type)
                                    .build()
                                    .execute(new StringCallback() {
                                        @Override
                                        public void onError(Request request, Exception e) {
                                            ToastUtils.showShort("提交失败");
                                        }
                                        @Override
                                        public void onResponse(String response) {

                                            if (r.equals(null)&&type.equals(null)){
                                                ToastUtils.showShort("queshao");
                                                return;
                                            }
                                            try{

                                                ToastUtils.showShort("提交成功");
                                                AlertDialog alertDialog = new AlertDialog.Builder(AfterSaleStateActivity.this).setTitle("提示").setMessage("提交成功")
                                                        .setNegativeButton("确认", new DialogInterface.OnClickListener() {
                                                            @Override
                                                            public void onClick(DialogInterface dialogInterface, int i) {
                                                                finish();
                                                            }
                                                        }).create();
                                                alertDialog.show();
                                            }catch (Exception e){
                                                e.printStackTrace();
                                                ToastUtils.showShort("提交失败");
                                            }
                                        }
                                    });
                        }catch (Exception e){
                            e.printStackTrace();
                        }

                    }

                }catch (Exception e){
                    e.printStackTrace();
                }





            }
        });






    }

    /**
     * 显示dialog menu
     * @param selectIndex 选择的位置索引
     * @param arr 展示的菜单项
     * @param title 标题
     * @param onClickListener 点击回调
     */
    private void show(int selectIndex, String[] arr, String title, final View.OnClickListener onClickListener) {
        final Dialog dialog = new Dialog(this, R.style.ActionSheetDialogStyle);
        //该数值表示需要显示的数据源
        View inflate = LayoutInflater.from(this).inflate(R.layout.dialog_sqyy, null);
        LinearLayout listLayout = (LinearLayout) inflate.findViewById(R.id.listLayout);
        TextView menuTitle = (TextView) inflate.findViewById(R.id.menuTitle);
        menuTitle.setText(title);

        View closeBtn = inflate.findViewById(R.id.closeBtn);
        closeBtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                dialog.dismiss();
            }
        });
        for (int i = 0; i < arr.length; i++) {
            View view = LayoutInflater.from(this).inflate(R.layout.item_dialog_list_layout, null);
            TextView textTitle = (TextView) view.findViewById(R.id.textTitle);
            final ImageView selectImage = (ImageView) view.findViewById(R.id.selectImage);
            textTitle.setText(arr[i]);
            if (selectIndex == i) {
                selectImage.setImageResource(R.drawable.porfile_icon_set_sel);
            } else {
                selectImage.setImageResource(R.drawable.porfile_icon_set_nor);
            }
            view.setTag(i);
            view.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    if (onClickListener != null) {
                        onClickListener.onClick(view);
                    }
                    selectImage.setImageResource(R.drawable.porfile_icon_set_sel);
                    dialog.dismiss();
                }
            });

            listLayout.addView(view);
        }
        //将布局设置给Dialog
        dialog.setContentView(inflate);
        //获取当前Activity所在的窗体
        Window dialogWindow = dialog.getWindow();
        //设置Dialog从窗体底部弹出
        dialogWindow.setGravity( Gravity.BOTTOM);
        //获得窗体的属性
        WindowManager.LayoutParams lp = dialogWindow.getAttributes();
        lp.y = 20;
        //设置Dialog距离底部的距离
        lp.width = WindowManager.LayoutParams.MATCH_PARENT;
        lp.height = WindowManager.LayoutParams.MATCH_PARENT;

//       将属性设置给窗体
        dialogWindow.setAttributes(lp);
        dialog.show();//显示对话框
        inflate.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                dialog.dismiss();
            }
        });

    }

    int clickType = -1;
    View.OnClickListener onClickListener = new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            selectMenuLayout.setVisibility(View.VISIBLE);
            tkMoneyLayout.setVisibility(View.VISIBLE);
            Button clickBtn = (Button) view;
            for (int i = 0; i < buttons.size(); i++) {
                buttons.get(i).setBackgroundResource(R.drawable.shape_button_mm);
                buttons.get(i).setTextColor(getResources().getColor(R.color.text_hsy_color));
                if (buttons.get(i) == clickBtn) {
                    init();
                    clickType = i;
                    if (i == 0) {
                        //点击仅退款
                        type = 1+"";
                        shopStateLayout.setVisibility(View.VISIBLE);
                        moneyResonLayout.setVisibility(View.VISIBLE);
                        weixiuResonLayout.setVisibility(View.GONE);
                        ll_moneytop.setVisibility(View.VISIBLE);
                        ll_moneybottem.setVisibility(View.VISIBLE);


                    } else if (i == 1) {
                        //点击换货
                        type = 3+"";
                        moneyResonLayout.setVisibility(View.GONE);
                        shopStateLayout.setVisibility(View.GONE);
                        weixiuResonLayout.setVisibility(View.VISIBLE);
                        ll_moneytop.setVisibility(View.GONE);
                        ll_moneybottem.setVisibility(View.GONE);

                    } else if (i == 2) {
                        //点击退款退货
                        type = 2+"";
                        shopStateLayout.setVisibility(View.GONE);
                        weixiuResonLayout.setVisibility(View.GONE);
                        moneyResonLayout.setVisibility(View.VISIBLE);
                        ll_moneytop.setVisibility(View.VISIBLE);
                        ll_moneybottem.setVisibility(View.VISIBLE);
                    } else if (i == 3) {
                        //点击维修
                        type = 4+"";
                        moneyResonLayout.setVisibility(View.GONE);
                        shopStateLayout.setVisibility(View.GONE);
                        weixiuResonLayout.setVisibility(View.VISIBLE);
                        ll_moneytop.setVisibility(View.GONE);
                        ll_moneybottem.setVisibility(View.GONE);
                    }
                }
            }
            clickBtn.setBackgroundResource(R.drawable.shape);
            clickBtn.setTextColor(getResources().getColor(R.color.text_red_zi));
        }
    };

    private void init(){
        reason = "";
        reason1 = "";
        selectMoneyState = -1;
        selectShopState = -1;
        weixiuResonState = -1;
        tv_hwzt.setText("");
        tv_tkyy.setText("");
        tv_sqyy.setText("");
    }


    @SuppressLint("HandlerLeak")
    public class GridAdapter extends BaseAdapter {
        private LayoutInflater inflater; // 视图容器
        private int selectedPosition = -1;// 选中的位置
        private boolean shape;

        public boolean isShape() {
            return shape;
        }

        public void setShape(boolean shape) {
            this.shape = shape;
        }

        public GridAdapter(Context context) {
            inflater = LayoutInflater.from(context);
        }

        public void update() {
            loading();
        }

        public int getCount() {
            return (Bimp.bmp.size() + 1);
        }

        public Object getItem(int arg0) {

            return null;
        }

        public long getItemId(int arg0) {

            return 0;
        }

        public void setSelectedPosition(int position) {
            selectedPosition = position;
        }

        public int getSelectedPosition() {
            return selectedPosition;
        }

        /**
         * ListView Item设置
         */
        public View getView(int position, View convertView, ViewGroup parent) {
            final int coord = position;
            AfterSaleStateActivity.GridAdapter.ViewHolder holder = null;
            if (convertView == null) {
                convertView = inflater.inflate(R.layout.item_published_grida,
                        parent, false);
                holder = new AfterSaleStateActivity.GridAdapter.ViewHolder();
                holder.image = (ImageView) convertView
                        .findViewById(R.id.item_grida_image);
                convertView.setTag(holder);
            } else {
                holder = (AfterSaleStateActivity.GridAdapter.ViewHolder) convertView.getTag();
            }

            if (position == Bimp.bmp.size()) {
                holder.image.setImageBitmap(BitmapFactory.decodeResource(
                        getResources(), R.drawable.icon_addpic_unfocused));
                if (position == 9) {
                    holder.image.setVisibility(View.GONE);
                }
            } else {
                holder.image.setImageBitmap(Bimp.bmp.get(position));
            }

            return convertView;
        }

        public class ViewHolder {
            public ImageView image;
        }

        Handler handler = new Handler() {
            public void handleMessage(Message msg) {
                switch (msg.what) {
                    case 1:
                        adapter.notifyDataSetChanged();
                        break;
                }
                super.handleMessage(msg);
            }
        };

        public void loading() {
            new Thread(new Runnable() {
                public void run() {
                    while (true) {
                        if (Bimp.max == Bimp.drr.size()) {
                            Message message = new Message();
                            message.what = 1;
                            handler.sendMessage(message);
                            break;
                        } else {
                            try {
                                String path = Bimp.drr.get(Bimp.max);
                                System.out.println(path);
                                Bitmap bm = Bimp.revitionImageSize(path);
                                Bimp.bmp.add(bm);
                                String newStr = path.substring(
                                        path.lastIndexOf("/") + 1,
                                        path.lastIndexOf("."));
                                FileUtils.saveBitmap(bm, "" + newStr);
                                Bimp.max += 1;
                                Message message = new Message();
                                message.what = 1;
                                handler.sendMessage(message);
                            } catch (IOException e) {

                                e.printStackTrace();
                            }
                        }
                    }
                }
            }).start();
        }
    }
    private static final int TAKE_PICTURE = 0x000000;
    protected void onRestart() {
        adapter.update();
        super.onRestart();
    }
    public class PopupWindows extends PopupWindow {

        public PopupWindows(Context mContext, View parent) {

            View view = View
                    .inflate(mContext, R.layout.item_popupwindows, null);
            view.startAnimation(AnimationUtils.loadAnimation(mContext,
                    R.anim.fade_ins));
            LinearLayout ll_popup = (LinearLayout) view
                    .findViewById(R.id.ll_popup);
            ll_popup.startAnimation(AnimationUtils.loadAnimation(mContext,
                    R.anim.push_bottom_in_2));

            setWidth(ViewGroup.LayoutParams.FILL_PARENT);
            setHeight(ViewGroup.LayoutParams.FILL_PARENT);
            setBackgroundDrawable(new BitmapDrawable());
            setFocusable(true);
            setOutsideTouchable(true);
            setContentView(view);
            showAtLocation(parent, Gravity.BOTTOM, 0, 0);
            update();

            Button bt1 = (Button) view
                    .findViewById(R.id.item_popupwindows_camera);
            Button bt2 = (Button) view
                    .findViewById(R.id.item_popupwindows_Photo);
            Button bt3 = (Button) view
                    .findViewById(R.id.item_popupwindows_cancel);
            bt1.setOnClickListener(new View.OnClickListener() {
                public void onClick(View v) {
                    photo();
                    dismiss();
                }

                private void photo() {
                    Intent openCameraIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
                    startActivityForResult(openCameraIntent, TAKE_PICTURE);
                }
                protected void onActivityResult(int requestCode, int resultCode, Intent data) {


                    switch (requestCode) {
                        case TAKE_PICTURE:
                            Bitmap bm = (Bitmap) data.getExtras().get("data");
                            String fileName = String.valueOf(System.currentTimeMillis());

                            FileUtils.saveBitmap(bm, fileName);
                            String imagePath = FileUtils.SDPATH + fileName + ".JPEG";
                            if (Bimp.drr.size() < 3 && resultCode == -1) {
                                Bimp.drr.add(imagePath);
                            }
                            break;
                    }
                }
            });
            bt2.setOnClickListener(new View.OnClickListener() {
                public void onClick(View v) {
                    Intent intent = new Intent(AfterSaleStateActivity.this,
                            TestPicActivity.class);
                    startActivity(intent);
                    dismiss();
                }
            });
            bt3.setOnClickListener(new View.OnClickListener() {
                public void onClick(View v) {
                    dismiss();
                }
            });

        }
    }

    @Override
    public void onActivityResult(int requestCode, int arg1, Intent arg2) {
        super.onActivityResult(requestCode, arg1, arg2);
        selectImageView.onActivityResult(requestCode, arg1, arg2);
    }

}
